package com.cenxi.utils;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.cenxi.dao.domain.ExcelDomain;
import com.cenxi.dao.domain.Excelc;
import com.cenxi.dao.domain.Excelcx;
import com.cx.minio.utils.RandomInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * excel工具
 *
 * @author admin
 * @date 2023/01/31
 */
@Slf4j
public class ExcelUtils {

    private ExcelUtils(){}

    /**
     * 下载Excel  流
     *
     * @param response 响应体
     * @param list 数据
     * @param clazz JavaBean反射对象
     * @param fileName 文件名 fileName = fileName + "_" + DateTimeUtils.getCurrentDateTime() + ".xlsx";
     * @param <T> 泛型
     */
    public static <T> void downLoad(HttpServletResponse response, List<T> list, Class<T> clazz, String fileName) {
        try {
            //设置响应的类型
            setResponse(response, URLEncoder.encode(fileName, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
//        setResponse(response, fileName);
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景设置为灰
        headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 默认表头
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        // 自适应列宽表头
        ExcelWidthStyleStrategy excelWidthStyleStrategy = new ExcelWidthStyleStrategy();
        try (OutputStream outputStream = response.getOutputStream()) {
//            EasyExcelFactory.write(outputStream, clazz).registerWriteHandler(horizontalCellStyleStrategy).sheet("sheet1").doWrite(list);
            EasyExcelFactory.write(outputStream, clazz).registerWriteHandler(excelWidthStyleStrategy)
                    .sheet().doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取最终的文件名
     *
     * @param fileName 文件初始名
     * @return 最终的文件名
     */
    public static String getFileName(String fileName) {
        return fileName + "_" + System.currentTimeMillis() + ".xlsx";
    }

    /**
     * 设置响应体参数
     *
     * @param response 响应对象
     * @param fileName 文件名
     */
    public static void setResponse(HttpServletResponse response, String fileName)  {
        response.reset();
        response.addHeader("content-type", "application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        response.addHeader("Cache-Control", "no-cache");
    }

    public static List<ExcelDomain> getExcelDomain() {
        List<ExcelDomain> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            ExcelDomain excelDomain = new ExcelDomain();
            excelDomain.setAddre(RandomInfo.getRandomAddress());
            excelDomain.setAge(RandomInfo.getRandomAge());
            String name;
            if (i%2==0){
                name = RandomInfo.getRandomBoyName();
            } else {
                name = RandomInfo.getRandomGirlName();
            }
            excelDomain.setName(name);
            excelDomain.setPhone(RandomInfo.getRandomPhone());
            list.add(excelDomain);
        }
        return list;
    }

    public static List<Excelcx> getExcecxl() {
        List<Excelcx> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            Excelcx excelDomain = new Excelcx();
            excelDomain.setAddre(RandomInfo.getRandomAddress());
            excelDomain.setAge(RandomInfo.getRandomAge());
            String name;
            if (i%2==0){
                name = RandomInfo.getRandomBoyName();
            } else {
                name = RandomInfo.getRandomGirlName();
            }
            excelDomain.setName(name);
            excelDomain.setPhone(RandomInfo.getRandomPhone());
            list.add(excelDomain);
        }
        return list;
    }

    public static List<Excelc> getExclc() {
        List<Excelc> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            Excelc excelDomain = new Excelc();
            excelDomain.setAddre(RandomInfo.getRandomAddress());
            excelDomain.setAge(RandomInfo.getRandomAge());
            String name;
            if (i%2==0){
                name = RandomInfo.getRandomBoyName();
            } else {
                name = RandomInfo.getRandomGirlName();
            }
            excelDomain.setName(name);
            excelDomain.setPhone(RandomInfo.getRandomPhone());
            list.add(excelDomain);
        }
        return list;
    }
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("11", "11");
        System.out.println(map.get("11"));
        System.out.println("----------");
        Map<String, String> map1 = new HashMap<>();
        BeanUtil.copyProperties(map, map1);
        System.out.println(map1.get("11"));
    }


}
